﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model.NotarialFormEnot
{
    public class ExportDTO
    {
        public string ExternalID {get; set;}
        public bool RecordDeleted  {get; set;}
        public string ChamberID  {get; set;}
        public string NotaryID  {get; set;}
        public DateTime OperationDate  {get; set;}
        public short OperationType  {get; set;}
        public string Note  {get; set;}
        public DateTime ActDate  {get; set;}
        public string ActNumber  {get; set;}
        public string DocRegnum  {get; set;}
        public string DocType  {get; set;}
        public DateTime DocDate  {get; set;}
        public string Blanks  {get; set;}
        public string ExternalData  {get; set;}
        public string RecordOwnerID { get; set; }
    }

    public class Configuration
    {
        public string ChamberID { get; set; }
        public string NotaryID { get; set; }
        public IDictionary<int, string> DocTypes { get; set; }
        public string GetDocType(int key)
        {
            if (DocTypes.Keys.Contains(key))
                return DocTypes[key];
            return "50";
        }
    }

    public enum OperationType : short
    {
        New = 101,
        Use = 102,

        ReturnNotUsed = 103,

        FoundBad = 104,
        FoundDublicate = 105,
        FoundDefect = 106,
        FoundLost = 107,
        FoundMissing = 108,

        ReturnBad = 109,
        ReturnDublicate = 110,
        ReturnDefect = 111,
    }
 
    public static class NotarialFormRangeExtensions
    {
        public static ExportDTO ToNotarialFormEnot(this NotarialFormRange range, Configuration configuration)
        {
            return new ExportDTO
            {
                ExternalID = string.Format("new{0}{1}", range.StartNumber, range.EndNumber),
                RecordDeleted = false,
                ChamberID = configuration.ChamberID,
                NotaryID = configuration.NotaryID,
                OperationDate = range.CreateDate,
                OperationType = (short)OperationType.New, 
                Note = "получено в палате",
                Blanks = string.Format("{0},{1},{2},7", range.NotarialFormSerialNumber.Name.Trim(), range.StartNumber, range.EndNumber),
            };
        }
    }

    public static class OrderExtensions
    {
        public static ExportDTO ToNotarialFormEnot(this Order order, NotarialServiceDataContext dc, Configuration configuration)
        {
            var forms = order.NotarialForm.Where(nf => nf.StatusId == (int)Model.NotarialForm.NotarialFormStatusType.Used && !nf.IsInEnot);

            if (!forms.Any()) return null;

            var result = new ExportDTO
                            {
                                ExternalID = "used" + order.Id,
                                RecordDeleted = false,
                                ChamberID = configuration.ChamberID,
                                NotaryID = configuration.NotaryID,
                                OperationDate = order.Finished.Value,
                                OperationType = (short)OperationType.Use,
                                Note = "использование бланков",
                                DocRegnum = string.Format("{0}/{1}", order.NotarialRegister.Name, order.NotarialRegisterRow),
                                DocType = configuration.GetDocType(order.NotarialActionId),
                                DocDate = order.Finished.Value,
                            };

            result.Blanks = GetBlanksString(forms.OrderBy(f => f.Name));

            return result;
        }

        private static string GetBlanksString(IEnumerable<NotarialForm> forms)
        {
            if (!forms.Any()) return string.Empty;

            var blanks = new StringBuilder();
            NotarialForm last = null;
            foreach (var nf in forms)
            {   
                if (last == null || (last.NotarialFormSerialNumber.Id != nf.NotarialFormSerialNumber.Id) || (last.Name + 1 != nf.Name))
                {
                    if (blanks.Length != 0 && last != null)
                        blanks.AppendFormat("{0},7;", last.Name);

                    blanks.AppendFormat("{0},{1},", nf.NotarialFormSerialNumber.Name.Trim(), nf.Name);
                }

                nf.IsInEnot = true;
                last = nf;
            }
            blanks.AppendFormat("{0},7", last.Name);
            return blanks.ToString();
        }
    }
}
